FROM golang:1.13.5-alpine as golang-builder
# 为我们的镜像设置必要的环境变量
ENV GO111MODULE=on \
	GOPROXY="https://goproxy.cn,direct"
# install git - apt-get replace with apk
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && apk update && \
    apk upgrade && \
    apk add --no-cache bash git openssh

# 设置编译目录
WORKDIR /home/project
COPY go.mod go.mod
COPY go.sum go.sum
RUN go mod download
# 将代码复制到容器中
COPY . .
# 将我们的代码编译成二进制可执行文件  可执行文件名为 app
RUN CGO_ENABLED=0 go build -o ./app .
# 运行镜像
FROM alpine:latest as prod
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && apk update && \
    apk upgrade && \
    apk add --no-cache bash tzdata
WORKDIR /home/www/
COPY --from=0 /home/project/ ./
ENTRYPOINT ["./app"]